package kamikaze.service.admin;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kamikaze.bean.User;
import kamikaze.service.UserSession;
import kamikaze.utils.Database;

public class ListAllUserServlt extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		UserSession.getFrom(req).asAdmin();

		List<User> userList = Database.get().execute(new Database.Template<List<User>>() {

			@Override
			public List<User> query(EntityManager em) throws Exception {

				return em.createQuery("select u from User u").getResultList();
			}
		});

		req.setAttribute("userList", userList);
		req.getRequestDispatcher("/WEB-INF/jsp/admin/list_user.jsp").forward(req, resp);

	}
}
